package com.datamation.swadeshi.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.control.ReferenceNum;
import com.datamation.swadeshi.control.data.SalOrdPrintDS;
import com.datamation.swadeshi.control.data.VanSalPrintDS;
import com.datamation.swadeshi.model.SalOrdPrintPre;
import com.datamation.swadeshi.model.VanSalPrintPre;

public class PrintVanSaleItemAdapter extends ArrayAdapter<VanSalPrintPre> {
	Context context;
	ArrayList<VanSalPrintPre> list;
	private VanSalPrintDS dsVanSalPrintDS;
	int totcases=0;
	int totpieces=0;
	int StrPrintCaseQtyVal=0;
	ReferenceNum referenceNum ;
	public PrintVanSaleItemAdapter(Context context, ArrayList<VanSalPrintPre> list) {

		super(context, R.layout.row_printitems_listview, list);
		this.context = context;
		this.list = list;
		dsVanSalPrintDS = new VanSalPrintDS(context);
		referenceNum =new  ReferenceNum(context);
	}

	@Override
	public View getView(int position, final View convertView,
			final ViewGroup parent) {

		LayoutInflater inflater = null;
		View row = null;
		ArrayList<VanSalPrintPre> vanSalprintList;
		vanSalprintList = dsVanSalPrintDS.getAllListPreview(referenceNum.getCurrentRefNo(context.getResources().getString(R.string.NumVal)));
		int ll = vanSalprintList.size();

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		row = inflater.inflate(R.layout.row_printitems_listview, parent, false);

		LinearLayout layout = (LinearLayout) row
				.findViewById(R.id.linearLayout);
		TextView itemname = (TextView) row.findViewById(R.id.printitemname);
		TextView transtype = (TextView) row.findViewById(R.id.printtrans);
		TextView caseqty = (TextView) row.findViewById(R.id.printcasesQty);
		TextView pieceqty = (TextView) row.findViewById(R.id.printpiecesQty);
		TextView amount = (TextView) row.findViewById(R.id.printamount);
		TextView unitprice = (TextView) row.findViewById(R.id.printunitprice);
		TextView units = (TextView) row.findViewById(R.id.printunits);
		TextView discount = (TextView) row.findViewById(R.id.printdiscount);
		TextView printindex = (TextView) row.findViewById(R.id.printindex);

		String Transtype = list.get(position).getVAN_PRINT_TRAN_TYPE();

		if (Transtype.equalsIgnoreCase("SA")) {
			Transtype = "Trans : SALES";
		} else if (Transtype.equalsIgnoreCase("MR")) {
			Transtype = "Trans : MARKET RETURN";
		} else if (Transtype.equalsIgnoreCase("UR")) {
			Transtype = "Trans : USABLE RETURN";
		} else if (Transtype.equalsIgnoreCase("FI")) {
			Transtype = "Trans : FREE ISSUE DISTRIBUTOR";
		} else if (Transtype.equalsIgnoreCase("FD")) {
			Transtype = "Trans : FREE ISSUE COMPANY";
		}

		String itemDisPer = list.get(position).getVAN_PRINT_DETAIL_DISPER();
		String itemDisVal = list.get(position).getVAN_DIS_VALUE_AMT();
		String itemDisPerVal = list.get(position).getVAN_PRINT_DISAMT_DETAIL();
		String DiscountRow = "(0.00)/(0.00)(0.00%)";

		if (itemDisPer.equalsIgnoreCase("0")) {
			itemDisPer = "(0.00%)";
		} else {
			itemDisPer = "("+ itemDisPer + "%)"	;
		}
		
		if(itemDisVal.equalsIgnoreCase("0")) {
			itemDisVal = "0.00";
		}else{
			itemDisVal = itemDisVal;
		}
		
		if(itemDisPerVal.equalsIgnoreCase("0")){
			itemDisPerVal = "0.00";
		}else{
			itemDisPerVal =itemDisPerVal;
		}
		
		StrPrintCaseQtyVal = Integer.parseInt(list.get(position).getVAN_PRINT_CASE_QTY().toString());
		totcases = totcases + StrPrintCaseQtyVal;
		Log.v("StrPrintCaseQtyVal", totcases + "");
		
		int StrPrintPieceQtyVal = Integer.parseInt(list.get(position).getVAN_PRINT_PIECE_QTY());
		totpieces = totpieces + StrPrintPieceQtyVal;
		Log.v("StrPrintPieceQtyVal", totpieces + "");
		
		
		DiscountRow =  itemDisVal + "/" + itemDisPerVal + itemDisPer;

		itemname.setText(list.get(position).getVAN_PRINT_ITEMNAME());
		transtype.setText(Transtype);
		caseqty.setText(list.get(position).getVAN_PRINT_CASE_QTY());
		pieceqty.setText(list.get(position).getVAN_PRINT_PIECE_QTY());
		amount.setText(list.get(position).getVAN_PRINT_DETAIL_AMT());
		unitprice.setText(list.get(position).getVAN_PRINT_SELL_PRICE());
		units.setText("U:" + list.get(position).getVAN_PRINT_UNITS());
		// discount.setText(list.get(position).getSAL_DIS_VALUE_AMT()+"/"+list.get(position).getSAL_PRINT_DISAMT_DETAIL()+"("+
		// list.get(position).getSAL_PRINT_DETAIL_DISPER()+"%)");
		discount.setText(DiscountRow);
		position = position +1;
		String pos = Integer.toString(position);
		printindex.setText(pos+".");

		Log.v("Position Item", position + "");

		totcases=0;
		StrPrintCaseQtyVal=0;
		return row;
	}
}
